<template>
  <div>
    <div class="yun"></div>
    <div class="loginMain" v-loading="words">
      <div class="words">
        <div ref="echartsWordcloud"></div>
      </div>
      <div class="loginBox">
        <div class="title">欢迎来到
          <div>群众呼声管理后台</div>
          <img src="https://wap.mala.cn/mala/imgs/114.png" class="logo"></div>
        <!--        <div class="screen">当前分辨率{{ screenWidth }} * {{ screenHeight }}</div>-->

        <el-tabs v-model="activeName" style="margin-top:20px;">
          <el-tab-pane label="账号密码登录" name="first">

            <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
                     label-position="left"
            >
              <el-form-item prop="username" class="el-form-item__content-focus">
                <span class="svg-container">
                  <svg-icon icon-class="user"/>
                </span>
                <el-input
                  v-model="loginForm.username"
                  placeholder="用户名"
                  ref="username"
                  name="username"
                  type="text"
                  tabindex="1"
                  auto-complete="on"
                />
              </el-form-item>

              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password"/>
                </span>
                <el-input
                  ref="password"
                  v-model="loginForm.password"
                  placeholder="密码"
                  name="password"
                  type="password"
                  tabindex="2"
                  auto-complete="on"
                />
              </el-form-item>

              <div id="geetest"></div>
              <el-button :loading="loginloading" type="primary" size="big"
                         style="width:100%;margin-bottom:30px;height:50px;"
                         @click.native.prevent="handleLogin"
              >登录到后台
              </el-button>
              <div class="tips">
                <div style="color:#999;text-align: center">Copyright 许小龙 Powered by wz.mala.cn</div>
                <div style="color:#999;text-align: center;font-size:12px;margin: 30px 0 5px 0">
                  Version:{{ versionData.version }}
                </div>
                <div style="color:#999;text-align: center;font-size:12px;">
                  LastBuild:{{ versionData.compileTime }}
                </div>
              </div>

            </el-form>


          </el-tab-pane>

        </el-tabs>

      </div>
    </div>
  </div>
</template>
<script>
import '@/utils/gt4'
import * as echarts from 'echarts'
import { getWord } from '@/api/count'
import { getGeet } from '@/api/user'
import versionJSON from '@/utils/versionJSON.json'
import 'echarts-wordcloud'

export default {
  name: 'Citys',
  data() {
    const validateUsername = (rule, value, callback) => {
      callback()
    }
    const validateGeet = (rule, value, callback) => {
      callback(new Error('密码格式不正确'))
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码格式不正确'))
      } else {
        callback()
      }
    }
    return {
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        loginAuth: [{ required: true, trigger: 'blur', validator: validateGeet }]
      },
      words: true,
      loginloading: false,
      activeName: 'first',
      screenWidth: window.screen.width,
      screenHeight: window.screen.height,
      loading: false,
      versionData: {},
      loginForm: {
        username: '',
        password: '',
        loginAuth: []
      }
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    this.Geet()
    this.versionData = { version: versionJSON.version, compileTime: this.format(versionJSON.compileTime) }
    this.$nextTick(() => {
      this.gWords()
    })
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          if (!this.loginForm.loginAuth.geetest_challenge) {
            this.$message('请完成滑动验证')
            return
          }
          this.loginloading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loginloading = false
          }).catch(() => {
            this.loginloading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    format(dataString) {
      //dataString是整数，否则要parseInt转换
      var time = new Date(dataString)
      var year = time.getFullYear()
      var month = time.getMonth() + 1
      var day = time.getDate()
      var hour = time.getHours()
      var minute = time.getMinutes()
      var second = time.getSeconds()
      return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
    },
    Geet() {
      let _this = this
      getGeet({ login: 1 }).then(response => {
        let data = response.data
        initGeetest({
          gt: data.gt,
          challenge: data.challenge,
          offline: !data.success,
          new_captcha: true,
          width: '100%'
        }, function(captchaObj) {
          captchaObj.appendTo('#geetest')
          captchaObj.onSuccess(() => {
            _this.$set(_this.loginForm, 'loginAuth', captchaObj.getValidate())
          })
        })
      }).catch(err => {
      })
    },
    makeWords(data) {

      const el = this.$refs.echartsWordcloud
      this.chart = echarts.init(el)
      let colorList = ['#819cd9', '#d3e4ff', '#d4d4ff']
      //按照colorList指定颜色的数据进行随机
      const option = {
        tooltip: {
          show: true
        },
        series: [
          {
            // shape: 'square',
            //用来调整词之间的距离
            gridSize: 9,
            rotationRange: [0, 0],
            //用来调整字的大小范围
            sizeRange: [12, 38],
            type: 'wordCloud',
            textPadding: 0,
            left: 'center',
            drawOutOfBound: false,
            shrinkToFit: true,
            layoutAnimation: false,

            // top: "center",
            textStyle: {
              color: function() {
                let index = Math.floor(Math.random() * 3)
                return colorList[index]
              }
              // fontFamily: '阿里妈妈数黑体 Bold'

            },
            data: data
          }
        ]
      }
      //设置图表的参数
      this.chart.setOption(option)
      this.words = false
    },
    gWords() {
      getWord().then(r => {
        if (r.code == 200) {
          this.makeWords(r.data)

        }
      })
    }
  }
}
</script>
<style>
#app {
  background-image: none !important;
  background: #000;
  height: 100vh;
  overflow: hidden;
}
</style>
<style lang="scss" scoped>
#geetest {
  width: 100%;
  height: 44px;
  margin-bottom: 22px;
  border: 10px;
}

$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$bg: #283443;
$light_gray: #333;
$cursor: #fff;


::v-deep {
  .el-loading-mask {
    background: transparent;
  }

  .el-form-item__content {
    line-height: 20px;

  }


  .loginBox {
    .login-form {
      position: relative;
      width: 520px;
      max-width: 100%;
      padding: 10px 0;
      margin: 0 auto;
      overflow: hidden;
    }

    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        padding-left: 15px;
        -webkit-appearance: none;
        border: 0;
        color: $light_gray;
        height: 47px;
        font-size: 14px;
      }

      input:-internal-autofill-previewed,
      input:-internal-autofill-selected {
        -webkit-text-fill-color: #000 !important;
        transition: background-color 5000s ease-in-out 0s !important;
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      border-radius: 5px;
      color: #454545;
      border: 1px solid #dde2e9;
      border-radius: 4px;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;

      .iconfont {
        font-size: 25px;
      }
    }

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
}

#geetest {
  margin-bottom: 20px;
}

@keyframes rowup {
  0% {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
  }
  50% {
    -webkit-transform: translate3d(0, -360px, 0);
    transform: translate3d(0, -360px, 0);
  }
  100% {
    -webkit-transform: translate3d(0, -780px, 0);
    transform: translate3d(0, -780px, 0);
  }
}

@keyframes yunduo {
  0% {
    background-size: 100%;
  }
  50% {
    background-size: 200%;
  }
  100% {
    background-size: 400%;
    //transform: translateX(980px);
  }

}

.words {
  position: relative;
  left: -100px;
  min-height: 100vh;
  -webkit-animation: 40s rowup alternate infinite;
  animation: 40s rowup alternate infinite;
  position: relative;

  > div {
    position: relative;
    width: calc(1400px - 476px);
    height: 4000px;
  }
}

.loginMain {
  position: relative;
  width: 1400px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  height: 100vh;
  justify-content: space-between;
}

.loginBox {
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, .05);
  box-sizing: border-box;
  height: 600px;
  padding: 48px 48px 32px;
  width: 476px;

  .screen {
    color: #999;
    text-align: center;
    font-size: 12px;
    margin: 20px 0 0;
  }

  .title {
    color: #0c0d0e;
    font-size: 28px;
    font-weight: 500;
    letter-spacing: .003em;
    line-height: 32px;
    text-align: left;
    position: relative;

    .logo {
      position: absolute;
      right: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 60px;
    }

    > div {
      color: #3854af;
      font-weight: bolder;
      margin: 10px 0;
    }
  }
}
//如果是手机
@media screen and (max-width: 768px) {
  .words{
    display: none;
  }
  .loginBox{
    width:calc(100vw - 40px);
    margin:0 20px;
    .title{
      font-size:22px;
    }
  }
}
</style>
